// Copyright 2019 Google LLC.

#include "modules/skparagraph/include/DartTypes.h"
#include "modules/skparagraph/include/ParagraphStyle.h"
#include "src/base/SkUTF.h"
#include "src/core/SkStringUtils.h"
#include <native_drawing/drawing_text_typography.h>

namespace skia {
namespace textlayout {

StrutStyle::StrutStyle() {
    fFontStyle = SkFontStyle::Normal();
    fFontSize = 14;
    fHeight = 1;
    fLeading = -1;
    fForceHeight = false;
    fHeightOverride = false;
    fHalfLeading = false;
    fEnabled = false;
}

const StrutStyle kDefaultStrutStyle;
const TextStyle kDefaultTextStyle;

TextIndent::TextIndent() {
    fFirstLine = 0.0;
    fRestLine = 0.0;
}

ParagraphStyle::ParagraphStyle() {
    fTextAlign = TextAlign::kStart;
    fTextDirection = TextDirection::kLtr;
    fLinesLimit = std::numeric_limits<size_t>::max();
    fHeight = 1;
    fTextHeightBehavior = TextHeightBehavior::kAll;
    fHintingIsOn = true;
    fReplaceTabCharacters = false;
    fStyleOHOS = OH_Drawing_CreateTypographyStyle();
    OH_Drawing_TypographyStyleSetHintsEnabled(fStyleOHOS, fHintingIsOn);
}

TextAlign ParagraphStyle::effective_align() const {
    return (TextAlign)OH_Drawing_TypographyStyleGetEffectiveAlignment(fStyleOHOS);
//     if (fTextAlign == TextAlign::kStart) {
//         return (fTextDirection == TextDirection::kLtr) ? TextAlign::kLeft : TextAlign::kRight;
//     } else if (fTextAlign == TextAlign::kEnd) {
//         return (fTextDirection == TextDirection::kLtr) ? TextAlign::kRight : TextAlign::kLeft;
//     } else {
//         return fTextAlign;
//     }
}

void ParagraphStyle::fillOHOSStyle() {
    if (!(fStrutStyle == kDefaultStrutStyle)) {
        setStrutStyle(fStrutStyle);
    }
    if (!fDefaultTextStyle.equals(kDefaultTextStyle)) {
        OH_Drawing_SetTypographyTextStyle(fStyleOHOS, fDefaultTextStyle);
    }
    if (fTextDirection != TextDirection::kLtr) {
        setTextDirection(fTextDirection);
    }
    if (fTextAlign != TextAlign::kStart) {
        setTextAlign(fTextAlign);
    }
    if (fLinesLimit != std::numeric_limits<size_t>::max()) {
        setMaxLines(fLinesLimit);
    }
    if (!fEllipsis.isEmpty()) {
        OH_Drawing_SetTypographyTextEllipsis(fStyleOHOS, fEllipsis.c_str());
    }
    if (!SkScalarNearlyEqual(fHeight, 1.0f)) {
        setHeight(fHeight);
    }
    if (fTextHeightBehavior != TextHeightBehavior::kAll) {
        setTextHeightBehavior(fTextHeightBehavior);
    }
    if (!fHintingIsOn) {
        turnHintingOff();
    }
}


}  // namespace textlayout
}  // namespace skia
